package com.mynetwork.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.mynetwork.utils.FileUtils;

import android.util.Log;

public class ContentInfo implements Serializable {
	private static final long serialVersionUID = -5582204347827868876L;
	private static final String TAG = "ProductInfo";
	
	public static final int TAG_NONE = 0;
	public static final int TAG_NEW = 1;
	public static final int TAG_HOT = 2;
	public static final int TAG_BEST = 3;
	public int idx;						//콘텐츠 IDX
	public int userIdx;						//유저 IDX
	public String title;					//제목
	public String contDesc;					//내용
	public String happenDate;				//사건 일자
	public String happenTime;			//사건 시간대
	
	public int readNum;				//조회수
	public String address;				//주소
	public double latitude;				//위도
	public double longitude;				//경도
	public String createdTime;					//등록시간
	public String modifiedTime;					//수정시간
	
	public int categoryIdx;				//카테고리 IDX
	public int isDel;				//삭제 여부
	public double distance;	//거리
	public int listCount;	//리스트 카운트
	

	public ArrayList<String> imgUrls = new ArrayList<String>();
	public boolean mIsMainLocalBackup = false;		//local에 main(coverUrl) image를 backup했는지 여부
	public boolean mIsDescLocalBackup = false;		//local에 desc(contImgUrl) image를 backup했는지 여부

	public ContentInfo(JSONObject e) throws JSONException {
		idx = e.getInt("idx");
		userIdx = e.getInt("userIdx");
		title = e.getString("title");
		contDesc = e.getString("contDesc");
		happenDate = e.getString("happenDate");
		happenTime = e.getString("happenTime");
		readNum = e.getInt("readNum");
		//issueYear = e.getInt("issueYear");
		//issueMonth = e.getInt("issueMonth");
		address = e.getString("address");
		//issueDay = e.getInt("issueDay");
		latitude = e.getDouble("latitude");
		longitude = e.getDouble("longitude");
		createdTime = e.getString("createdTime");
		modifiedTime = e.getString("modifiedTime");
		categoryIdx = e.getInt("categoryIdx");
		isDel = e.getInt("isDel");
		distance = e.getDouble("distance");
		listCount = e.getInt("listCount");
		JSONArray array = e.getJSONArray("imgUrls");
		
		for(int i=0;i < array.length();i++){
			imgUrls.add(array.getString(i));
        }
	}
	
	private int parserTag(String tag) {
		if (tag.equalsIgnoreCase("N")) {
			return TAG_NEW;
		} else if (tag.equalsIgnoreCase("H")) {
			return TAG_HOT;
		} else if (tag.equalsIgnoreCase("B")) {
			return TAG_BEST;
		}
		return TAG_NONE;
	}
	
    public static void WriteInfo(String path, ArrayList<ContentInfo> datas) {
    	if (datas.size() <= 0) {
    		FileUtils.deleteFile(path);
    		return;
    	}
    	
    	try {
    		FileOutputStream fos = null;
   			fos = new FileOutputStream(path);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(datas);
			for(ContentInfo info:datas) {
//	    		Log.e(TAG,"KDS3393_Info write " + info.imgUrls);
	    	}
			oos.close();
		} catch (Exception e) {
			Log.e(TAG,"" + e);
		}
    }
    
    public static ArrayList<ContentInfo> ReadInfo(String filename) {
    	ArrayList<ContentInfo> datas;
    	try {
    		File file = null;
    		file = new File(filename);
    		if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois = new ObjectInputStream(fis);
				datas = (ArrayList<ContentInfo>)ois.readObject();
				ois.close();
				String path;
				int count = datas.size();
				
				for(int i=0;i<count;i++) {
					ContentInfo info = datas.get(i);
					path = AppData.CONTENT_IMAGE_BACKUP_PATH + info.imgUrls.get(i).substring(info.imgUrls.lastIndexOf("/"), info.imgUrls.get(i).length());
					info.mIsMainLocalBackup = FileUtils.fileIsLive(path);
				}
				return datas;
    		}
		} catch (Exception e) {
			Log.e(TAG,"" + e);
		}
    	return new ArrayList<ContentInfo>();
    }
}